#ifndef HTTPAUTHDAEMON_H
#define HTTPAUTHDAEMON_H




#include <QTcpServer>
#include <QPointer>
#include "singleapplication.h"
#include <QtCore>
#include <QtNetwork>

static const int WakeupPort = 8080;

#define _SERVER_ \
             QString("HttpAuthDaemon PPK version 1.3")
#define _CACHE_ \
             QString("public, max-age=0")
#define _POWER_ \
             QString("PHR/22.4.0-qt")
             
             

#ifdef Q_OS_WIN
#include <qt_windows.h>
#else
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#endif

inline static int takepid()
{
    #ifdef Q_OS_WIN
    return (int)104;
    #else
    return (int)getpid();
    #endif
}

inline static QString login()
{
	static QString login;
	if(login.isEmpty())
	{
#ifdef Q_WS_QWS
		login = QLatin1String("qws");
#endif
#ifdef Q_OS_WIN
		QT_WA({
			wchar_t buffer[256];
			DWORD bufferSize = sizeof(buffer) / sizeof(wchar_t) - 1;
			GetUserNameW(buffer, &bufferSize);
			login = QString::fromUtf16((ushort*)buffer);
		},
		{
			char buffer[256];
			DWORD bufferSize = sizeof(buffer) / sizeof(char) - 1;
			GetUserNameA(buffer, &bufferSize);
			login = QString::fromLocal8Bit(buffer);
		});
#else
		struct passwd* pwd = getpwuid(getuid());
		if(pwd)
			login = QLatin1String(pwd->pw_name);
#endif
		Q_ASSERT(!login.isEmpty());
	}

	return login;
}
             


class HttpAuthDaemon : public QTcpServer
{
     Q_OBJECT
//
public:
      HttpAuthDaemon( const QString icmd , QObject* parent=0  );
      inline void pause() {  disabled = TRUE; wake(); }
      inline void resume() { disabled = FALSE; wake(); }
      inline int port() { return WakeupPort; }
      QString TimeStampMail();
protected:
      bool disabled;
      bool running;

      //////QTcpSocket * mSocket;

private:
    QString os_key;
signals:

     void processClose();
     void processRestart();

private Q_SLOTS:
    
    void newConnectionIncomming();
    void ReadandSend();
    void wake();

public Q_SLOTS:
    
    void setCmd( const QString op );

};





class Server_Application : public QCoreApplication
{
    Q_OBJECT
//
public:
 Server_Application( int &argc, char **argv )
  : QCoreApplication(argc, argv),server(0)
{
    setApplicationName(_SERVER_);
    QString serverName = QCoreApplication::applicationName().replace(" ","_");
    os_key = serverName + "_" + login();
    Q_ASSERT(!os_key.isEmpty());
    QStringList cmdlist = arguments();
    cmdlist.removeFirst();
    cmdlist.prepend(os_key);
    initcmd = "null";
    if (cmdlist.size() > 0)  { 
        initcmd = cmdlist.join("|");
    }
}


QString serverName() const
{
   return os_key;
}
QString commands() const
{
   return initcmd;
}

void start()
{
    server = new HttpAuthDaemon(commands(),this); 
    qDebug() << "Server should now listen on localhost:" << server->port() <<  " running as os_key->" << os_key;
    qDebug( "Hit CTRL-C to quit." );
    qDebug() << "Or kill pid " << takepid();
    connect(server, SIGNAL(processClose()),this, SLOT(quit()));
    connect(server, SIGNAL(processRestart()),this, SLOT(reload()));
}
protected:
private:
    HttpAuthDaemon *server;
    QString os_key;
    QString initcmd;
private Q_SLOTS:
    void reload()
   {
     if (server) {
        qDebug() << "Reindex config on  pid " << takepid();
     }
   }
public Q_SLOTS:
void handleCommands( const QString cmd )
{
    qDebug() << "handleCommands on pid " << takepid();
   if (server) {
       server->setCmd(cmd);
   }
}

};

//
#endif // HTTPAUTHDAEMON_H

